package com.healthcloud.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.healthcloud.dao.AllergicHistoryMapper;
import com.healthcloud.entity.AllergicHistory;
import com.healthcloud.service.AllergicHistoryService;
import com.healthcloud.utils.Pagine;

/**
 * Created by XYC on 2015/7/15.
 */
@Service
public class AllergicHistoryServiceImpl implements AllergicHistoryService {

    @Autowired
    private AllergicHistoryMapper allergicHistoryMapper;

    /**
     * 保存或是更新用户过敏史相关信息
     * @param allergicHistory
     * @param operate         true 为只存，false保存、修改
     * @return
     */
    @Override
    public AllergicHistory saveOrUpdata(AllergicHistory allergicHistory, Boolean operate) {

//        ArrayList<AllergicHistory> allergicHistories = allergicHistoryMapper.findByEntityWithOutPage(allergicHistory);
//        ArrayList<AllergicHistory> allergicHistories = findByIdCard(allergicHistory);
        if (allergicHistory.getOnSetTimeOfString()!=null){
            allergicHistory.setOnSetTime(allergicHistory.StringTransformDateTime(allergicHistory.getOnSetTimeOfString()+" 00:00:00"));
        }
        if(operate){
            allergicHistory.setCreatedTime(new Date());
            allergicHistory.setUpdatedTime(new Date());
            allergicHistoryMapper.save(allergicHistory);
        }else {
            AllergicHistory allergicHistoryTemp =  allergicHistoryMapper.get(allergicHistory.getId());
//            allergicHistory.setId(allergicHistoryTemp.get(0).getId());
            allergicHistory.setUpdatedTime(new Date());
            allergicHistory.setCreatedTime(allergicHistoryTemp.getCreatedTime());
            allergicHistoryMapper.update(allergicHistory);
        }
        return allergicHistory;
    }


    /**
     * 保存用户过敏史相关信息，数组形式。将旧信息删除，新信息保存
     * @param allergicHistories
     * @return
     */
    @Override
    public Boolean saveEntityList(ArrayList<AllergicHistory> allergicHistories) {
        if (allergicHistories.isEmpty()){
            return false;
        }else{
            deletByIdCard(allergicHistories.get(0));
            listAddDate(allergicHistories);
            allergicHistoryMapper.saveByEntities(allergicHistories);
        }
        return true;
    }

    /**
     *根据过敏史相关字段进行查询不带翻页信息
     * @param allergicHistory
     * @return
     */
    @Override
    public ArrayList<AllergicHistory> findByEntity(AllergicHistory allergicHistory) {
        ArrayList<AllergicHistory> allergicHistories = allergicHistoryMapper.findByEntityWithOutPage(allergicHistory);
        return allergicHistories;
    }

    /**
     * 根据过敏史相关字段进行查询带翻页信息
     * @param allergicHistory
     * @param pagine
     * @return
     */
    @Override
    public ArrayList<AllergicHistory> findByEntity(AllergicHistory allergicHistory, Pagine<AllergicHistory> pagine) {
        ArrayList<AllergicHistory> allergicHistories =(ArrayList<AllergicHistory>) allergicHistoryMapper.findByEntity(allergicHistory, pagine);
        return allergicHistories;
    }

    /**
     * 根据过敏史中病人身份证号进行查询不带翻页信息
     * @param allergicHistory
     * @return
     */
    @Override
    public ArrayList<AllergicHistory> findByIdCard(AllergicHistory allergicHistory) {
        AllergicHistory allergicHistory1 = new AllergicHistory();
        allergicHistory1.setIdCard(allergicHistory.getIdCard());
        ArrayList<AllergicHistory> allergicHistories = findByEntity(allergicHistory1);
        return allergicHistories;
    }

    /**
     * 根据过敏史中病人身份证号进行查询带翻页信息
     * @param allergicHistory
     * @param pagine
     * @return
     */
    @Override
    public ArrayList<AllergicHistory> findByIdCard(AllergicHistory allergicHistory, Pagine<AllergicHistory> pagine) {
        AllergicHistory allergicHistory1 = new AllergicHistory();
        allergicHistory1.setIdCard(allergicHistory.getIdCard());
        ArrayList<AllergicHistory> allergicHistories = findByEntity(allergicHistory1,pagine);
        return allergicHistories;
    }

    /**
     * 根据过敏史中用户身份证号进行数据的删除
     * @param allergicHistory
     * @return
     */
    @Override
    public Boolean deletByIdCard(AllergicHistory allergicHistory) {
        AllergicHistory allergicHistory1=new AllergicHistory();
        allergicHistory1.setIdCard(allergicHistory.getIdCard());
        deletByEntity(allergicHistory1);
        return null;
    }

    /**
     * 根据用户实体类进行数据删除
     * @param allergicHistory
     * @return
     */
    @Override
    public Boolean deletByEntity(AllergicHistory allergicHistory) {
        allergicHistoryMapper.deletByEntity(allergicHistory);
        return true;
    }

    /**
     * 根据过敏史表中ID字段进行数据删除
     * @param idData
     * @return
     */
    @Override
    public Boolean deleByIDs(String idData) {
        String[] idDatas = idData.split("-");
        for (int i = 0; i < idDatas.length ; i++) {
//            Long.parseLong(idDatas[i]);
            allergicHistoryMapper.delete(Long.parseLong(idDatas[i]));
        }
        return null;
    }


    @Override
    public void listAddDate(ArrayList<AllergicHistory> allergicHistories) {
        for (int i=0;i<allergicHistories.size();i++){
            if (allergicHistories.get(i).getOnSetTimeOfString()!=null){
                allergicHistories.get(i).setOnSetTime(allergicHistories.get(i).StringTransformDateTime(allergicHistories.get(i).getOnSetTimeOfString().substring(0,10) + " 00:00:00"));
            }
            allergicHistories.get(i).setUpdatedTime(new Date());
            allergicHistories.get(i).setCreatedTime(new Date());
        }
    }

    @Override
    public void fillInAttribute(HashMap<String, HashMap> dictHashMap, ArrayList<AllergicHistory> allergicHistories) {
        HashMap<Integer,String> allergiciHashMap = dictHashMap.get("allergichis");
        HashMap<Integer,String> allergenHashMap = dictHashMap.get("allergen");
        for (int i = 0; i < allergicHistories.size(); i++) {
            allergicHistories.get(i).setAllergenName(allergenHashMap.get(allergicHistories.get(i).getAllergen()));
            allergicHistories.get(i).setAllergicName(allergiciHashMap.get(allergicHistories.get(i).getAllergic()));
        }
    }

    @Override
    public void changeShowTime(ArrayList<AllergicHistory> allergicHistories) {
        for (int i = 0; i < allergicHistories.size(); i++) {
            if(allergicHistories.get(i).getOnSetTime() ==null)
            {
                continue;
            }
            allergicHistories.get(i).setOnSetTimeOfString(allergicHistories.get(i).DateTimeTransformString("yyyy-MM-dd",allergicHistories.get(i).getOnSetTime()));
        }
    }

}
